//
//  zhuche.m
//  MY QQapp
//
//  Created by liuchengming on 16/11/1.
//  Copyright © 2016年 liuchengming. All rights reserved.
//

#import "zhuche.h"
#import "ViewController.h"

@interface zhuche ()
@property (weak, nonatomic) IBOutlet UITextField *uesrname;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UITextField *tel;

@end

@implementation zhuche

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
}
- (IBAction)zhuche:(id)sender
{
    //ViewController * con = [self.storyboard instantiateViewControllerWithIdentifier:@"no_1"];
    NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"http://xl18z.cn/node/reg?name=%@&password=%@&tel=%@",_uesrname.text,_password.text,_tel.text]];
    NSURLRequest * req = [NSURLRequest requestWithURL:url];
    NSURLSession * session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask * task = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
    {
        NSString * text = [[NSString alloc]initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
        NSLog(@"%@",text);
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        //大中枢派发
        dispatch_async(dispatch_get_main_queue(), ^
                       {
                           if ([dict[@"errcode"]intValue] == 0)
                           {
                               NSString *str = [dict objectForKey:@"errinfo"];
                               UIAlertController *aler =[UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
                               [self presentViewController:aler animated:YES completion:^{
                                   
                               }];
                               UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                                   
                               }];
                               [aler addAction:action];
                               
                           }else
                           {
                               NSString * str = [dict objectForKey:@"errinfo"];
                               UIAlertController * aler = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
                               [self presentViewController:aler animated:YES completion:^
                               {
                                   
                               }];
                               UIAlertAction * action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action)
                               {
                                   
                               }];
                               [aler addAction:action];
                               NSLog(@"%@",dict[@"errinfo"]);
                           }
                       });
        }];
    [task resume];
    
    
    //[self.navigationController pushViewController:con animated:YES];
}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_uesrname resignFirstResponder];
    [_password resignFirstResponder];
    [_tel resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
